<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品sku码" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入产品sku码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属用户的登录名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入归属用户的登录名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属用户的昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入归属用户的昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外部系统的sku" prop="sellerSku">
        <el-input
          v-model="queryParams.sellerSku"
          placeholder="请输入外部系统的sku"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品中文名称" prop="nameCn">
        <el-input
          v-model="queryParams.nameCn"
          placeholder="请输入产品中文名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品英文名称" prop="nameEn">
        <el-input
          v-model="queryParams.nameEn"
          placeholder="请输入产品英文名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="海关编码" prop="customsCode">
        <el-input
          v-model="queryParams.customsCode"
          placeholder="请输入海关编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报价值" prop="declaredPric">
        <el-input
          v-model="queryParams.declaredPric"
          placeholder="请输入申报价值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原产地" prop="originAddress">
        <el-input
          v-model="queryParams.originAddress"
          placeholder="请输入原产地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="producSpecifications">
        <el-input
          v-model="queryParams.producSpecifications"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input
          v-model="queryParams.length"
          placeholder="请输入长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宽度" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入宽度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高度" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入高度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input
          v-model="queryParams.color"
          placeholder="请输入颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否包含电池 Y=包含 N=不包含" prop="battery">
        <el-select v-model="queryParams.battery" placeholder="请选择是否包含电池 Y=包含 N=不包含" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="goodsType">
        <el-input
          v-model="queryParams.goodsType"
          placeholder="请输入产品类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许分销,Y=允许,N=不允许" prop="consignment">
        <el-select v-model="queryParams.consignment" placeholder="请选择是否允许分销,Y=允许,N=不允许" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分销截止时间格式为 2024-07-13 00:00:00" prop="consignmentEndTime">
        <el-date-picker clearable
          v-model="queryParams.consignmentEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择分销截止时间格式为 2024-07-13 00:00:00">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分销最低零售价" prop="consignmentMinRetaiPrice">
        <el-input
          v-model="queryParams.consignmentMinRetaiPrice"
          placeholder="请输入分销最低零售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:produce:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:produce:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:produce:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:produce:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            v-hasPermi="['wms:produce:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="ckg2"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="cleadRedis"
            v-hasPermi="['wms:produce:cleadRedis']"
        >清空缓存</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="produceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="产品sku码" align="center" prop="sku" />
      <el-table-column label="归属用户的登录名称" align="center" prop="username" />
      <el-table-column label="归属用户的头像url" align="center" prop="avatarUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatarUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="归属用户的昵称" align="center" prop="nickName" />
      <el-table-column label="外部系统的sku" align="center" prop="sellerSku" />
      <el-table-column label="产品中文名称" align="center" prop="nameCn" />
      <el-table-column label="产品英文名称" align="center" prop="nameEn" />
      <el-table-column label="产品图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="海关编码" align="center" prop="customsCode" />
      <el-table-column label="申报价值" align="center" prop="declaredPric" />
      <el-table-column label="原产地" align="center" prop="originAddress" />
      <el-table-column label="产品规格" align="center" prop="producSpecifications" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="长度" align="center" prop="length" />
      <el-table-column label="宽度" align="center" prop="width" />
      <el-table-column label="高度" align="center" prop="height" />
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="是否包含电池 Y=包含 N=不包含" align="center" prop="battery">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.battery"/>
        </template>
      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="goodsType" />
      <el-table-column label="产品的备注描述,最多2550字符" align="center" prop="description" />
      <el-table-column label="是否允许分销,Y=允许,N=不允许" align="center" prop="consignment">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.consignment"/>
        </template>
      </el-table-column>
      <el-table-column label="分销截止时间格式为 2024-07-13 00:00:00" align="center" prop="consignmentEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.consignmentEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分销备注" align="center" prop="consignmentDescription" />
      <el-table-column label="分销最低零售价" align="center" prop="consignmentMinRetaiPrice" />
      <el-table-column label="分销的详情" align="center" prop="consignmentText" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:produce:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:produce:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品sku码" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入产品sku码" />
        </el-form-item>
        <el-form-item label="归属用户的登录名称" prop="username">
          <el-input v-model="form.username" placeholder="请输入归属用户的登录名称" />
        </el-form-item>
        <el-form-item label="归属用户的头像url" prop="avatarUrl">
          <image-upload v-model="form.avatarUrl"/>
        </el-form-item>
        <el-form-item label="归属用户的昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入归属用户的昵称" />
        </el-form-item>
        <el-form-item label="外部系统的sku" prop="sellerSku">
          <el-input v-model="form.sellerSku" placeholder="请输入外部系统的sku" />
        </el-form-item>
        <el-form-item label="产品中文名称" prop="nameCn">
          <el-input v-model="form.nameCn" placeholder="请输入产品中文名称" />
        </el-form-item>
        <el-form-item label="产品英文名称" prop="nameEn">
          <el-input v-model="form.nameEn" placeholder="请输入产品英文名称" />
        </el-form-item>
        <el-form-item label="产品图片" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
        <el-form-item label="海关编码" prop="customsCode">
          <el-input v-model="form.customsCode" placeholder="请输入海关编码" />
        </el-form-item>
        <el-form-item label="申报价值" prop="declaredPric">
          <el-input v-model="form.declaredPric" placeholder="请输入申报价值" />
        </el-form-item>
        <el-form-item label="原产地" prop="originAddress">
          <el-input v-model="form.originAddress" placeholder="请输入原产地" />
        </el-form-item>
        <el-form-item label="产品规格" prop="producSpecifications">
          <el-input v-model="form.producSpecifications" placeholder="请输入产品规格" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="长度" prop="length">
          <el-input v-model="form.length" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽度" />
        </el-form-item>
        <el-form-item label="高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入高度" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-input v-model="form.color" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="是否包含电池 Y=包含 N=不包含" prop="battery">
          <el-select v-model="form.battery" placeholder="请选择是否包含电池 Y=包含 N=不包含">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="goodsType">
          <el-input v-model="form.goodsType" placeholder="请输入产品类型" />
        </el-form-item>
        <el-form-item label="产品的备注描述,最多2550字符" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否允许分销,Y=允许,N=不允许" prop="consignment">
          <el-select v-model="form.consignment" placeholder="请选择是否允许分销,Y=允许,N=不允许">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分销截止时间格式为 2024-07-13 00:00:00" prop="consignmentEndTime">
          <el-date-picker clearable
            v-model="form.consignmentEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择分销截止时间格式为 2024-07-13 00:00:00">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="分销备注" prop="consignmentDescription">
          <el-input v-model="form.consignmentDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分销最低零售价" prop="consignmentMinRetaiPrice">
          <el-input v-model="form.consignmentMinRetaiPrice" placeholder="请输入分销最低零售价" />
        </el-form-item>
        <el-form-item label="分销的详情">
          <editor v-model="form.consignmentText" :min-height="192"/>
        </el-form-item>
        <el-form-item label="创建者" prop="createBody">
          <el-input v-model="form.createBody" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listProduce, getProduce, delProduce, addProduce, updateProduce,cleadRedis } from "@/api/wms/produce";
import {getToken} from "@/utils/auth";
export default {
  name: "Produce",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      produceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "id",
        isAsc: "desc",
        sku: null,
        username: null,
        avatarUrl: null,
        nickName: null,
        sellerSku: null,
        nameCn: null,
        nameEn: null,
        img: null,
        customsCode: null,
        declaredPric: null,
        originAddress: null,
        producSpecifications: null,
        weight: null,
        length: null,
        width: null,
        height: null,
        color: null,
        battery: null,
        goodsType: null,
        description: null,
        consignment: null,
        consignmentEndTime: null,
        consignmentDescription: null,
        consignmentMinRetaiPrice: null,
        consignmentText: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sku: [
          { required: true, message: "产品sku码不能为空", trigger: "blur" }
        ],
        nameCn: [
          { required: true, message: "产品中文名称不能为空", trigger: "blur" }
        ],
        nameEn: [
          { required: true, message: "产品英文名称不能为空", trigger: "blur" }
        ],
      },
      // 导入参数
        upload: {
          // 是否显示弹出层（导入）
          open: false,
          // 弹出层标题（导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/wms/produce/importData"
        },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listProduce(this.queryParams).then(response => {
        this.produceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sku: null,
        username: null,
        avatarUrl: null,
        nickName: null,
        sellerSku: null,
        nameCn: null,
        nameEn: null,
        img: null,
        customsCode: null,
        declaredPric: null,
        originAddress: null,
        producSpecifications: null,
        weight: null,
        length: null,
        width: null,
        height: null,
        color: null,
        battery: null,
        goodsType: null,
        description: null,
        consignment: null,
        consignmentEndTime: null,
        consignmentDescription: null,
        consignmentMinRetaiPrice: null,
        consignmentText: null,
        createBody: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduce(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduce(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduce(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品编号为"' + ids + '"的数据项？').then(function() {
        return delProduce(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/produce/export', {
        ...this.queryParams
      }, `produce_${new Date().getTime()}.xlsx`)
    },
/** 下载模板操作 */
importTemplate() {
  this.download('wms/produce/importTemplate', {
  }, `template_${new Date().getTime()}.xlsx`)
},
/** 导入按钮操作 */
handleImport() {
  this.upload.title = "产品导入";
  this.upload.open = true;
},
// 文件上传中处理
handleFileUploadProgress(event, file, fileList) {
  this.upload.isUploading = true;
},
// 文件上传成功处理
handleFileSuccess(response, file, fileList) {
  this.upload.open = false;
  this.upload.isUploading = false;
  this.$refs.upload.clearFiles();
  this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
  this.getList();
},
// 提交上传文件
submitFileForm() {
  this.$refs.upload.submit();
},
/** 清空所有缓存 */
cleadRedis() {
  cleadRedis().then(response => {
    this.$modal.msgSuccess("清空成功");
  });
},
  }
};
</script>
